package com.gx.common.result;

import com.gx.common.enums.ResultCodeEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * @Author Liu-pj
 * @Date 2022/2/22 14:47
 * 统一结果响应
 **/
@Data
public class AppResult<T> implements Serializable {
    private static final Logger log = LoggerFactory.getLogger(AppResult.class);

    @ApiModelProperty(value = "状态码", example = "200")
    private Integer code;

    @ApiModelProperty(value = "响应信息")
    private String msg;

    @ApiModelProperty(value = "数据")
    private T data;

    public static <T> AppResult<T> success() {
        AppResult<T> serverResponseEntity = new AppResult<>();
        serverResponseEntity.setCode(ResultCodeEnum.SUCCESS.getCode());
        serverResponseEntity.setMsg(ResultCodeEnum.SUCCESS.getMessage());
        return serverResponseEntity;
    }

    public static <T> AppResult<T> success(T data) {
        AppResult<T> serverResponseEntity = new AppResult<>();
        serverResponseEntity.setData(data);
        serverResponseEntity.setCode(ResultCodeEnum.SUCCESS.getCode());
        return serverResponseEntity;
    }


    public static <T> AppResult<T> failure(ResultCodeEnum resultCodeEnum) {
        AppResult<T> serverResponseEntity = new AppResult<>();
        serverResponseEntity.setMsg(resultCodeEnum.getMessage());
        serverResponseEntity.setCode(resultCodeEnum.getCode());
        return serverResponseEntity;
    }

    public static <T> AppResult<T> failure(ResultCodeEnum resultCodeEnum, T data) {
        AppResult<T> serverResponseEntity = new AppResult<>();
        serverResponseEntity.setMsg(resultCodeEnum.getMessage());
        serverResponseEntity.setCode(resultCodeEnum.getCode());
        serverResponseEntity.setData(data);
        return serverResponseEntity;
    }

    public static <T> AppResult<T> failure(String msg, Integer code) {
        AppResult<T> serverResponseEntity = new AppResult<>();
        serverResponseEntity.setMsg(msg);
        serverResponseEntity.setCode(code);
        return serverResponseEntity;
    }

    public static <T> AppResult<T> failure(String msg) {
        AppResult<T> serverResponseEntity = new AppResult<>();
        serverResponseEntity.setMsg(msg);
        serverResponseEntity.setCode(ResultCodeEnum.UNKNOWN_ERROR.getCode());
        return serverResponseEntity;
    }
}
